$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'camera/list',
        datatype: "json",
        colModel: [			
			{ label: 'cameraId', name: 'cameraId', index: 'camera_id', width: 50, key: true },
			{ label: '名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '编号', name: 'number', index: 'number', width: 80 }, 			
			{ label: '经度', name: 'longitude', index: 'longitude', width: 80 }, 			
			{ label: '纬度', name: 'latitude', index: 'latitude', width: 80 }, 			
			{ label: '养殖户名字', name: 'herdsmanName', index: 'herdsman_name', width: 80 }, 			
			{ label: '养殖户标识', name: 'herdsmanId', index: 'herdsman_id', width: 80 }, 			
			{ label: '在线状态', name: 'onlineState', index: 'online_state', width: 80 }, 			
			{ label: '状态更新时间', name: 'stateUpdateTime', index: 'state_update_time', width: 80 }, 			
			{ label: 'hls地址', name: 'hlsurl', index: 'hlsUrl', width: 80 }, 			
			{ label: 'pic地址', name: 'picurl', index: 'picUrl', width: 80 }, 			
			{ label: 'rtmp地址', name: 'rtmpurl', index: 'rtmpUrl', width: 80 }, 			
			{ label: '安装时间', name: 'installTime', index: 'install_time', width: 80 }, 			
			{ label: '设备类型', name: 'typeCamera', index: 'type_camera', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        starttime:'',
        endtime:'',
        name:'',
		camera: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
        timequery: function (event) {
            $("#jqGrid").jqGrid('setGridParam',{
                url:baseURL + 'camera/timelist',
                postData:{
                    'starttime':vm.starttime,
                    'endtime':vm.endtime,
                    'name':vm.name
                },
                datattype:'json',
                mtyp:'GET',
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
            }).trigger('reloadGrid');


        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.camera = {};
		},
		update: function (event) {
			var cameraId = getSelectedRow();
			if(cameraId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(cameraId)
		},
		saveOrUpdate: function (event) {
			var url = vm.camera.cameraId == null ? "camera/save" : "camera/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.camera),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var cameraIds = getSelectedRows();
			if(cameraIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "camera/delete",
                    contentType: "application/json",
				    data: JSON.stringify(cameraIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(cameraId){
			$.get(baseURL + "camera/info/"+cameraId, function(r){
                vm.camera = r.camera;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});